Skip to content

Conversation

@0xfk0
Copy link

@0xfk0 0xfk0 commented Dec 8, 2024

This commit fixes issue #110407 in Bolt subproject: BOLT programs not support CREL-type relocations.

This commit fixes issue llvm#110407 in Bolt subproject:
BOLT programs not support CREL-type relocations.
@github-actions
Copy link

github-actions bot commented Dec 8, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the BOLT label Dec 8, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 8, 2024

@llvm/pr-subscribers-bolt

Author: 2:5030/1559 (0xfk0)

Changes

This commit fixes issue #110407 in Bolt subproject: BOLT programs not support CREL-type relocations.


Full diff: https://github.com/llvm/llvm-project/pull/119150.diff

1 Files Affected:

  • (modified) bolt/lib/Rewrite/RewriteInstance.cpp (+10-2)
diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index 76e1f0156f828d..f1ef97888a26ce 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -1938,8 +1938,11 @@ Error RewriteInstance::readSpecialSections() {
                   "Use -update-debug-sections to keep it.\n";
   }
 
-  HasTextRelocations = (bool)BC->getUniqueSectionByName(
-      ".rela" + std::string(BC->getMainCodeSectionName()));
+  const char *code_sec = BC->getMainCodeSectionName();
+  HasTextRelocations = !!BC->getUniqueSectionByName(std::string{".rela"} + code_sec);
+  if (!HasTextRelocations)
+    HasTextRelocations = !!BC->getUniqueSectionByName(std::string{".crel"} + code_sec);
+
   HasSymbolTable = (bool)BC->getUniqueSectionByName(".symtab");
   EHFrameSection = BC->getUniqueSectionByName(".eh_frame");
 
@@ -2127,6 +2130,11 @@ int64_t getRelocationAddend(const ELFObjectFile<ELFT> *Obj,
     llvm_unreachable("unexpected relocation section type");
   case ELF::SHT_REL:
     break;
+  case ELF::SHT_CREL: {
+    auto ERela = Obj->getCrel(Rel);
+    Addend = ERela.r_addend;
+    break; 
+  }
   case ELF::SHT_RELA: {
     const Elf_Rela *RelA = Obj->getRela(Rel);
     Addend = RelA->r_addend;

@ayermolo
Copy link
Contributor

ayermolo commented Dec 8, 2024

Please add a test.

Also for background crel is a new compact relocation that was proposed: https://discourse.llvm.org/t/rfc-crel-a-compact-relocation-format-for-elf/77600

@yota9
Copy link
Member

yota9 commented Dec 18, 2024

I think not only reading, but patching them is needed after binary processing

@yota9 yota9 self-requested a review December 18, 2024 11:06
@Andarwinux
Copy link
Contributor

I think not only reading, but patching them is needed after binary processing

If I understand correctly, there is no need to update relocations metadata unless a second round of instrumentation/optimization is needed for the instrumented/optimized binary?

I can use this PR to optimize a clang built with CREL.

@yota9
Copy link
Member

yota9 commented Dec 18, 2024

I think not only reading, but patching them is needed after binary processing

If I understand correctly, there is no need to update relocations metadata unless a second round of instrumentation/optimization is needed for the instrumented/optimized binary?

I can use this PR to optimize a clang built with CREL.

It seems I misunderstood and CREL is currently used for static relocations only. Then yes, the patch looks good to me, except for test absence :)

@github-actions
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff eff0d8103c5e0db938550dd6e18230ea8ed9ff4b e76d7498b2d85eb555a09e6b08b1b48c865df1ce --extensions cpp -- bolt/lib/Rewrite/RewriteInstance.cpp
View the diff from clang-format here.
diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index f1ef97888a..10dc1c66fc 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -1939,9 +1939,11 @@ Error RewriteInstance::readSpecialSections() {
   }
 
   const char *code_sec = BC->getMainCodeSectionName();
-  HasTextRelocations = !!BC->getUniqueSectionByName(std::string{".rela"} + code_sec);
+  HasTextRelocations =
+      !!BC->getUniqueSectionByName(std::string{".rela"} + code_sec);
   if (!HasTextRelocations)
-    HasTextRelocations = !!BC->getUniqueSectionByName(std::string{".crel"} + code_sec);
+    HasTextRelocations =
+        !!BC->getUniqueSectionByName(std::string{".crel"} + code_sec);
 
   HasSymbolTable = (bool)BC->getUniqueSectionByName(".symtab");
   EHFrameSection = BC->getUniqueSectionByName(".eh_frame");
@@ -2133,7 +2135,7 @@ int64_t getRelocationAddend(const ELFObjectFile<ELFT> *Obj,
   case ELF::SHT_CREL: {
     auto ERela = Obj->getCrel(Rel);
     Addend = ERela.r_addend;
-    break; 
+    break;
   }
   case ELF::SHT_RELA: {
     const Elf_Rela *RelA = Obj->getRela(Rel);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants